﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Cosmetic;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
using System.Collections.Generic;
namespace WebApp.Shopping
{
    public partial class checkOut  : PageBase
    {
        private Order m_Order;
        private UserManager m_UserManager = new UserManager();
        PurchaseManager thePurchaseManager = new PurchaseManager();
        ProductManager theProductManager = new ProductManager();
        infoManger theinfoManger = new infoManger();
        OrderManager theOrderManger = new OrderManager();
        SaleManager m_salemanager = new SaleManager();
        private infor m_infor = new infor();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProvince();
                string a = Convert.ToString(Session["userid"]);
                if (theOrderManger.CheckExistemail(new Guid(a)) != null)
                {
                    
                    Cosmetic.DataEntity.Order theUser = this.theOrderManger.CheckExistemail(new Guid(a));
                    this.txtReceiverName.Text = theUser.DeliveryUserName;
                    if (DDLProvince.Items.Count == 0)
                    {
                        //this.DDLProvince.Text = AddressManager.GetCityName(adds[num].Province);
                    }
                    else
                    {
                        this.DDLProvince.SelectedItem.Text = theUser.adress1;
                        if (this.DDLCity.Text != "请选择……")
                        {
                            DataTable dtc = CityManager.SelectAllCityByProvinceid(DDLProvince.SelectedValue); //.SelectAllCity();

                            this.DDLCity.Items.Clear();
                            this.DDLCity.Items.Add("请选择……");
                            for (int i = 0; i < dtc.Rows.Count; i++)
                            {

                                ListItem li = new ListItem(dtc.Rows[i]["city"].ToString(), dtc.Rows[i]["cityid"].ToString());
                                DDLCity.Items.Add(li);
                            }
                            this.DDLCity.SelectedItem.Text = theUser.adress2;
                        }

                    }
                    if (DDLCity.Items.Count == 0)
                    {

                    }
                    else
                    {
                        if (this.DDLCity.Text != "请选择……")
                        {
                            DataTable dta = CityManager.SelectAllAreaByCityid(DDLCity.SelectedValue);
                            this.ddlArea.Items.Clear();
                            this.ddlArea.Items.Add("请选择……");
                            for (int i = 0; i < dta.Rows.Count; i++)
                            {
                                ListItem li = new ListItem(dta.Rows[i]["area"].ToString(), dta.Rows[i]["areaid"].ToString());
                                ddlArea.Items.Add(li);
                            }

                            this.ddlArea.SelectedItem.Text = theUser.adress3;
                        }

                    }
                    this.txtAddress.Text = theUser.adress4;
                    this.txtDistrictNo.Text = theUser.tel1;
                    this.txtPhoneNo.Text = theUser.tel2;
                    this.txtPostCode.Text = theUser.DeliveryPostCode;
                    this.txtRemark.Text = theUser.Remark;
                    this.txtCellPhoneNo.Text = theUser.DeliveryCellPhone;
                    this.txtAddress1.Text = theUser.DeliveryAddress1;
                    this.txtAddress2.Text = theUser.DeliveryAddress2;
                    this.txtReceiverName.Text = theUser.DeliveryUserName;

                }
                string b = Convert.ToString(Session["tel"]);
                if (b!= null)
                {
                    this.txtCellPhoneNo.Text = b;
                }
                if (Session["Order"]!=null )
                {
                    Order m_Order = (Order)Session["Order"];
                if (DDLProvince.Items.Count == 0)
                    {
                        //this.DDLProvince.Text = AddressManager.GetCityName(adds[num].Province);
                    }
                    else
                    {
                        this.DDLProvince.SelectedItem.Text = m_Order.adress1;
                        if (this.DDLCity.Text != "请选择……")
                        {
                            DataTable dtc = CityManager.SelectAllCityByProvinceid(DDLProvince.SelectedValue); //.SelectAllCity();

                            this.DDLCity.Items.Clear();
                            this.DDLCity.Items.Add("请选择……");
                            for (int i = 0; i < dtc.Rows.Count; i++)
                            {

                                ListItem li = new ListItem(dtc.Rows[i]["city"].ToString(), dtc.Rows[i]["cityid"].ToString());
                                DDLCity.Items.Add(li);
                            }
                            this.DDLCity.SelectedItem.Text = m_Order.adress2;
                        }

                    }
                if (DDLCity.Items.Count == 0)
                {

                }
                else
                {
                    if (this.DDLCity.Text != "请选择……")
                    {
                        DataTable dta = CityManager.SelectAllAreaByCityid(DDLCity.SelectedValue);
                        this.ddlArea.Items.Clear();
                        this.ddlArea.Items.Add("请选择……");
                        for (int i = 0; i < dta.Rows.Count; i++)
                        {
                            ListItem li = new ListItem(dta.Rows[i]["area"].ToString(), dta.Rows[i]["areaid"].ToString());
                            ddlArea.Items.Add(li);
                        }

                        this.ddlArea.SelectedItem.Text = m_Order.adress3;
                    }
                }
                  this.txtAddress.Text = m_Order.adress4;
                  this.txtDistrictNo.Text = m_Order.tel1;
                  this.txtPhoneNo.Text = m_Order.tel2;
                  this.txtPostCode.Text = m_Order.DeliveryPostCode;
                  this.txtRemark.Text = m_Order.Remark;
                  this.txtCellPhoneNo.Text = m_Order.DeliveryCellPhone;
                  this.txtAddress1.Text = m_Order.DeliveryAddress1;
                  this.txtAddress2.Text = m_Order.DeliveryAddress2;
                  this.txtReceiverName.Text = m_Order.DeliveryUserName;

                }
            }

        }
        /// <summary>
        /// 绑定省。
        /// </summary>
        private void BindProvince()
        {
            DataTable dt = CityManager.SelectAllProvinces();
            this.DDLProvince.Items.Clear();
            this.DDLProvince.Items.Add("请选择……");
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                ListItem li = new ListItem(dt.Rows[i]["province"].ToString(), dt.Rows[i]["provinceid"].ToString());
                DDLProvince.Items.Add(li);
                
            }
        }

        private decimal GetDeliveryPrive(string area)
        {
            int weight = GetProductsWeight();

            decimal deliveryPrice = theProductManager.GetDeliveryPriceByWeightAndArea(weight, area);

            return deliveryPrice;
        }

        private int GetProductsWeight()
        {
            int totalweight = 0;
            if (Session["buycart"] != null)
            {
                Hashtable ht = Session["buycart"] as Hashtable;
                IList products = theProductManager.GetProducts(ht);
                int a = Convert.ToInt32(Session["count"]);
                foreach (var item in products)
                {
                    totalweight = (Convert.ToInt32(item.GetType().GetProperty("ProductWeight").GetValue(item, null)) * a);
                }
            }
            return totalweight;
        }


        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

            m_Order = (Order)Session["Order"];
            string a = Convert.ToString(Session["userid"]);

            if (theOrderManger.CheckExistemail(new Guid(a)) == null)
            {
                string city = this.DDLProvince.SelectedItem.Text;
                
                    m_Order.DeliveryType = "快递 ";
                    //m_Order.DeliveryPrice = "";
                 
                    Session["DeliveryPrice"] = GetDeliveryPrive(city).ToString();

               
               
                m_Order.DeliveryUserName = this.txtReceiverName.Text.Trim();
                m_Order.DeliveryAddress = this.DDLProvince.SelectedItem.Text + this.DDLCity.SelectedItem.Text + this.txtAddress.Text.Trim();//this.DropDownList1.SelectedItem.Text+"  "+this.DropDownList2.SelectedItem.Text+"  "+this.txtDeliveryAddress.Text.Trim();
                m_Order.DeliveryPostCode = this.txtPostCode.Text.Trim();
                m_Order.DeliveryTel = this.txtDistrictNo.Text.Trim() + "-" + this.txtPhoneNo.Text.Trim();
                m_Order.DeliveryCellPhone = Session["tel"].ToString();
                m_Order.adress1 = this.DDLProvince.SelectedItem.Text;
                m_Order.adress2 = this.DDLCity.SelectedItem.Text;
                m_Order.adress3 = this.ddlArea.SelectedItem.Text;
                m_Order.adress4 = this.txtAddress.Text;
                m_Order.tel1 = this.txtDistrictNo.Text;
                m_Order.tel2 = this.txtPhoneNo.Text;
                m_Order.Remark = this.txtRemark.Text;
                m_Order.DeliveryAddress1 = this.txtAddress1.Text;
                m_Order.DeliveryAddress2 = this.txtAddress2.Text;
                m_Order.DeliveryPrice = Convert.ToDecimal(Session["DeliveryPrice"]);
               
                Session["Order"] = m_Order;
            }
            else
            {
                m_Order.DeliveryUserName = this.txtReceiverName.Text.Trim();
                m_Order.DeliveryAddress = this.DDLProvince.SelectedItem.Text + this.DDLCity.SelectedItem.Text + this.txtAddress.Text.Trim();//this.DropDownList1.SelectedItem.Text+"  "+this.DropDownList2.SelectedItem.Text+"  "+this.txtDeliveryAddress.Text.Trim();
                m_Order.DeliveryPostCode = this.txtPostCode.Text.Trim();
                m_Order.DeliveryTel = this.txtDistrictNo.Text.Trim() + "-" + this.txtPhoneNo.Text.Trim();
                m_Order.DeliveryCellPhone = this.txtCellPhoneNo.Text.Trim();
                m_Order.adress1 = this.DDLProvince.SelectedItem.Text;
                m_Order.adress2 = this.DDLCity.SelectedItem.Text;
                m_Order.adress3 = this.ddlArea.SelectedItem.Text;
                m_Order.adress4 = this.txtAddress.Text;
                m_Order.tel1 = this.txtDistrictNo.Text;
                m_Order.tel2 = this.txtPhoneNo.Text;
                m_Order.Remark = this.txtRemark.Text;
                m_Order.DeliveryAddress1 = this.txtAddress1.Text;
                m_Order.DeliveryAddress2 = this.txtAddress2.Text;
                string city = this.DDLProvince.SelectedItem.Text;
                    m_Order.DeliveryType = "快递 ";
                   

                    Session["DeliveryPrice"] = GetDeliveryPrive(city).ToString();
                m_Order.DeliveryPrice = Convert.ToDecimal(Session["DeliveryPrice"]);

                Session["Order"] = m_Order;
                
            }
            Response.Redirect("~/Shopping/express.htm");
        }


        protected void DDLProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = CityManager.SelectAllCityByProvinceid(DDLProvince.SelectedValue);
            this.DDLCity.Items.Clear();
            this.DDLCity.Items.Add("请选择……");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem li = new ListItem(dt.Rows[i]["city"].ToString(), dt.Rows[i]["cityid"].ToString());
                DDLCity.Items.Add(li);
            }
            this.ddlArea.Items.Clear();
            this.ddlArea.Items.Add("请选择……");

        }

        protected void DDLCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = CityManager.SelectAllAreaByCityid(DDLCity.SelectedValue);

            this.ddlArea.Items.Clear();
            this.ddlArea.Items.Add("请选择……");
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                ListItem li = new ListItem(dt.Rows[i]["area"].ToString(), dt.Rows[i]["areaid"].ToString());
                ddlArea.Items.Add(li);
            }

        }
        //用我的帐户支付
        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    if (Session["password"]==null )
        //    {

        //        this.Response.Redirect(String.Format("~/logins.aspx?R={0}", this.Request.Url.PathAndQuery));
                
        //    }
        //    else 
        //    {
        //  User theuser = m_UserManager.GetUserById(this.SignInUser.UserId);
            
        //    m_Order = (Order)Session["Order"];
        //    if (CheckBox2.Checked)
        //    {
        //        Label1.Visible = true;
             
        //        decimal allprice = m_Order.Price;
        //        if (allprice -    theuser.Balance < 0)
        //        {

        //            Label1.Text = Convert.ToString(theuser.Balance - allprice);
        //            Label3.Visible = false;
        //            theuser.Balance = Convert.ToDecimal(theuser.Balance - allprice);
        //            UserManager.UpdateInformation(this.SignInUser.UserId, theuser.Balance);
        //            m_Order.Price = Convert.ToDecimal("0");
        //            theOrderManger.Update();
        //            Cosmetic.WebGeneric.WebMessageBox("恭喜您使用账户支付成功！我的账户还剩为:" + theuser.Balance + "!", this.Page);
        //        }
        //        else
        //        {
        //            m_Order.Price = allprice - theuser.Balance;
        //            theOrderManger.Update();
        //            this.Label1.Text = "0";
        //            this.Label3.Visible = false;
        //            theuser.Balance = Convert.ToDecimal("0");
        //            UserManager.UpdateInformation(this.SignInUser.UserId, theuser.Balance);
        //            Cosmetic.WebGeneric.WebMessageBox("您的帐户余额不足!请选择其他方式支付您需要在支付:" + allprice + "-" + theuser.Balance + "!", this.Page);

        //        }
        //}
        //}
        //}

        //protected void LinkButton10_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect("~/Space/MyaccountManager/cardnumbers.aspx");
        //}

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("shopCarts.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.TextBox2.Text == null)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请输入优惠券卡号!');", true);
                return;
            }
            Cosmetic.DataEntity.youhui theUser = this.m_salemanager.CheckExistes(this.TextBox2.Text.Trim());
            if (null == theUser)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('输入的卡号错误!');", true);
                return;
            }
            else
            {
                m_Order = (Order)Session["Order"];
                youhui theyouhui = m_salemanager.getyouhui(this.TextBox2.Text);
                if (theyouhui.value  == true)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('此优惠券已经用过!');", true);
                    return;
                }
                m_Order.Price = m_Order.Price * (m_salemanager.GetDiscountRatiof1(this.TextBox2.Text));
                Session["Order"] = m_Order;
                Session["ordernum"] = this.TextBox2.Text;
                PlaceHolder1.Visible = true;
                Literal1.Text = Convert.ToString(m_Order.Price);
                theyouhui.userid  = this.SignInUser.UserId;
                m_salemanager.Update();
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('恭喜您使用优惠券成功，打折后商品的总价格为" + m_Order.Price + "!');", true);
                return;
            }

        }

       

    }
}
